<template>
  <BasicModal v-bind="$attrs" @register="register" :title="getTitle" width="1000px">
    <a-spin :spinning="loading" size="large" tip="Loading...">
      <a-form ref="formRef" :disabled="disabled" class="antd-modal-form">
        <p style="text-align: center">
          <span style="width: 35%; text-align: center; font: 1.7em sans-serif">{{ itemName }}检测原始记录</span>
        </p>
        <div v-if="formData.labRecordSamList.length > 0">
          <labSamTable ref="registerSamTable" :labRecordSamList="formData.labRecordSamList"></labSamTable>
        </div>
        <!-- <div v-if="formData.labRecordDevList.length > 0"> -->
        <labDevTable ref="registerDevTable" :labRecordDevList="formData.labRecordDevList"></labDevTable>
        <!-- </div> -->
        <div v-if="formData.labRecordNormList.length > 0">
          <labNormTable ref="registerNormTable" :labRecordNormList="formData.labRecordNormList"></labNormTable>
        </div>
        <div v-if="formData.labRecordTargList.length > 0">
          <labTargTable ref="registeTargTable" :labRecordTargList="formData.labRecordTargList"></labTargTable>
        </div>
        <div v-if="formData.labRecordPerList.length > 0">
          <labPerTable ref="registerPerTable" :labRecordPerList="formData.labRecordPerList"></labPerTable>
        </div>
      </a-form>
    </a-spin>
    <template v-slot:footer>
      <a-button type="default" @click="closeModal"> 取消</a-button>
      <a-button type="primary" @click="handleSubmit('保存')">保存</a-button>
      <a-button type="primary" @click="handleSubmit('提交')">提交</a-button>
    </template>
  </BasicModal>
</template>

<script lang="ts" setup>
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { ref, reactive, onMounted } from 'vue';
  import labSamTable from './subTables/LabCheckItemsSam.vue';
  import labDevTable from './subTables/LabCheckItemsDev.vue';
  import labNormTable from './subTables/LabCheckItemsNorm.vue';
  import labTargTable from './subTables/LabCheckItemsTarget.vue';
  import labPerTable from './subTables/LabCheckItemsPerson.vue';
  import { queryItemData, saveOrUpdate } from './LabCheckItemsList.api';
  import { useMessage } from '/@/hooks/web/useMessage';

  const registerSamTable = ref();
  const registerDevTable = ref();
  const registerNormTable = ref();
  const registeTargTable = ref();
  const registerPerTable = ref();
  const loading = ref(true);
  const itemName = ref('');
  const formRef = ref();
  const disabled = ref(false);
  const emit = defineEmits(['success']);
  const { createMessage } = useMessage();
  const getTitle = ref('检测原始记录');
  const formData = reactive<Record<string, any>>({
    entrustId: '',
    taskItemId: '',
    itemId: '',
    itemName: '',
    itemStatus:'',
    samName: '',
    auditId: '',
    auditName: '',
    labRecordSamList: [],
    labRecordDevList: [],
    labRecordNormList: [],
    labRecordTargList: [],
    labRecordPerList: [],
  });

  const [register, { setModalProps, closeModal }] = useModalInner((data) => {
    setModalProps({
      minHeight: 950,
      showOkBtn: !data.disabled,
      // showOkBtn: data.isUpdate
    });
     console.log(data);
    disabled.value = data.disabled;
    itemName.value = data.record.itemName;
    formData.entrustId = data.record.entrustId;
    formData.taskItemId = data.record.id;
    formData.itemId = data.record.itemId;
    formData.itemName = data.record.itemName;
    formData.samName = data.record.samName;
    formData.auditId = data.record.auditId;
    formData.auditName = data.record.auditName;

    let params = {
      entrustId: data.record.entrustId,
      taskItemId: data.record.id,
      itemId: data.record.itemId,
    };
    queryItemData(params)
      .then((res) => {
        formData.labRecordSamList = res.labRecordSamList;
        formData.labRecordDevList = res.labRecordDevList;
        formData.labRecordNormList = res.labRecordNormList;
        formData.labRecordTargList = res.labRecordTargList;
        formData.labRecordPerList = res.labRecordPerList;
      })
      .finally(() => {
        loading.value = false;
      });
  });

  // setTimeout(() => {

  //       }, 800);

  //提交表单数据
  async function handleSubmit(state) {
    if(state==='提交'){
			formData.itemStatus = '待复核';
		}else {
			formData.itemStatus = '待实验';
		}
    // formData.auditId = registerPerTable.value.formPerData.auditId;
    // formData.auditName = registerPerTable.value.formPerData.auditName;
    // if (formData.auditName != '' && formData.auditName != null) {
    console.log('formData', formData);
    // console.log(registerNormTable.value.props.labRecordNormList)
    if (formData.labRecordSamList.length > 0) {
      formData.labRecordSamList = registerSamTable.value.props.labRecordSamList;
    }
    formData.labRecordDevList = registerDevTable.value.props.labRecordDevList;
    if (formData.labRecordNormList.length > 0) {
      formData.labRecordNormList = registerNormTable.value.props.labRecordNormList;
    }
    if (formData.labRecordTargList.length > 0) {
      formData.labRecordTargList = registeTargTable.value.props.labRecordTargList;
    }
    if (formData.labRecordPerList.length > 0) {
      formData.labRecordPerList = registerPerTable.value.props.labRecordPerList;
    }
    loading.value = true;
    await saveOrUpdate(formData)
      .then((res) => {
        createMessage.success(res.message);
      })
      .finally(() => {
        closeModal();
        emit('success');
      });
    // } else {
    //   createMessage.warning('请选择复核人员!');
    // }
  }
</script>

<style scoped>
  .header {
    line-height: 35px;
    background-color: #f2f2f2;
    color: #666;
  }

  .tr {
    height: 50px;
    border: 1px solid #a8aeb2;
  }

  .thA {
    width: 20%;
    text-align: center;
    border: 1px solid #a8aeb2;
    font: 1.2em sans-serif;
  }

  .thB {
    width: 30%;
    text-align: center;
    border: 1px solid #a8aeb2;
    font: 1.2em sans-serif;
  }

  .thC {
    width: 8%;
    text-align: center;
    border: 1px solid #a8aeb2;
    font: 1.2em sans-serif;
  }

  .thD {
    width: 32%;
    text-align: center;
    border: 1px solid #a8aeb2;
    font: 1.2em sans-serif;
  }

  .thE {
    width: 25%;
    text-align: center;
    border: 1px solid #a8aeb2;
    font: 1.2em sans-serif;
  }

  .thF {
    width: 75%;
    text-align: center;
    border: 1px solid #a8aeb2;
    font: 1.2em sans-serif;
  }

  .input {
    border: none;
    background-color: inherit;
    outline: none;
    border-bottom: none;
  }
</style>
